package config;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.render.ViewType;
import com.jfinal.template.Engine;

import controller.ControlController;
import controller.LoginController;
import controller.TestController;
import controller.UserController;
import controller.WeatherController;
import handler.LogHandler;
import model.Admin;
import model.Control;
import model.Site;
import model.User;
import model.Weather;
import thread.MyThread;
/**
 * function：实现登录验证，注册，网络连接通信
 *
 * technology：使用Jfinal进行web开发
 * 使用Mysql数据库通过C3P0数据连接池
 * 使用netty网络通信
 * 使用Json数据传输
 * 
 * data：2017.09.04
 * author：cyc
 */
public class MainConfig extends JFinalConfig {

	@Override
	public void configConstant(Constants me) {
		
		me.setViewType(ViewType.JSP);
		me.setDevMode(true);
		PropKit.use("config.properties");
		
	}

	@Override
	public void configRoute(Routes me) {

		me.add("/",LoginController.class); //登录界面
		me.add("/user",UserController.class); //用户界面
		me.add("/cyc",TestController.class); //测试界面
		me.add("/weather",WeatherController.class); //环境界面
		me.add("/control",ControlController.class);//控制器界面
	}

	@Override
	public void configEngine(Engine me) {

	}

	@Override
	public void configPlugin(Plugins me) {
		// 插件管理器配置
		C3p0Plugin  c3p0Plugin = new C3p0Plugin(PropKit.get("jdbcUrl"),PropKit.get("user"),PropKit.get("password"));
		ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
		arp.setShowSql(true);
		//数据库映射
		arp.addMapping("user", User.class);
		arp.addMapping("admin",Admin.class);
		arp.addMapping("weather",Weather.class);
		arp.addMapping("site",Site.class);
		arp.addMapping("control", Control.class);
		me.add(c3p0Plugin);
		me.add(arp);
	}

	@Override
	public void configInterceptor(Interceptors me) {
		// 拦截器配置，拦截action，@Clear清除拦截，Global、Inject、Class、Method四个层次

	}

	@Override
	public void configHandler(Handlers me) {
		// 处理器配置，接管Web请求
		me.add(new LogHandler());
	}
	
	
	//测试用
	public static void main(String[] args){
		JFinal.start("WebRoot", 8080, "/");
	}
	
	public void afterJFinalStart(){
		System.out.println("开启线程连接LoRa云服务器！");
		MyThread thread = new MyThread();
		thread.start();
	}
	
	public void beforeJFinalStop(){
		System.out.println("应用服务器关闭 Bye！");
	}

}
